package java_thinking.unit_21.Exercise.thread.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @Desc:   以下是一个带方法的类，它设置了 ScheduledExecutorService ，在 1 小时内每 10 秒钟蜂鸣一次：
 * @author: cww
 * @DateTime: 2019-08-05 22:45
 */

public class ScheduledExecutorServiceTest {
    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public static void beepForAnHour() {
        final Runnable beeper = new Runnable() {
            public void run() { System.out.println("beep"); }
        };
        final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0, 10, SECONDS);
        scheduler.schedule(() -> { beeperHandle.cancel(true); }, 60, SECONDS);
    }

    public static void main(String[] args) {
        beepForAnHour();
    }

}
